﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.SharePoint.Commands;
using SPVSX.Explorer.Extensions;
using Microsoft.SharePoint;
using SPVSX.Explorer.Extensions.NodeInformation;
using System.IO;
using SPVSX.Explorer.Extensions.CommandIds;

namespace SPVSX.Explorer.Commands
{
    /// <summary>
    /// The theme gallery commands
    /// </summary>
    class ThemeGalleryCommands
    {
        /// <summary>
        /// Gets data for each theme on the SharePoint site, and returns an array of 
        /// serializable objects that contain the data.
        /// </summary>
        /// <param name="context">The command context</param>
        /// <returns>The theme infos</returns>
        [SharePointCommand(ThemeGalleryCommandIds.GetThemes)]
        private static ThemeNodeInfo[] GetThemes(ISharePointCommandContext context)
        {
            var nodeInfos = new List<ThemeNodeInfo>();
            SPListItemCollection themes = context.Site.GetCatalog(
                SPListTemplateType.ThemeCatalog).Items;

            foreach (SPListItem theme in themes)
            {
                FileInfo file = new FileInfo(theme.File.Name);
                string name = file.Name.Replace(file.Extension, "");

                ThemeNodeInfo nodeInfo = new ThemeNodeInfo
                {
                    Id = theme.ID,
                    SiteId = theme.ParentList.ParentWeb.ID,
                    Name = name,
                    UniqueId = theme.UniqueId,
                    ImageUrl = theme.ParentList.ImageUrl,
                    Url = theme.Url
                };
                nodeInfos.Add(nodeInfo);
            }

            return nodeInfos.ToArray();
        }

        /// <summary>
        /// Gets data for each theme on the SharePoint site, and returns an array of 
        /// serializable objects that contain the data.
        /// </summary>
        /// <param name="context">The command context</param>
        /// <returns>The theme infos</returns>
        [SharePointCommand(ThemeGalleryCommandIds.GetThemeGallery)]
        private static ThemeGalleryNodeInfo GetThemeGallery(ISharePointCommandContext context)
        {
            SPList themeGallery = context.Site.GetCatalog(
                SPListTemplateType.ThemeCatalog);

            ThemeGalleryNodeInfo nodeInfo = new ThemeGalleryNodeInfo
            {
                Id = themeGallery.ID,
                SiteId = themeGallery.ParentWeb.ID,
                Name = themeGallery.Title,
                Url = themeGallery.DefaultViewUrl
            };

            return nodeInfo;
        }

        /// <summary>
        /// Gets additional property data for a specific theme.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="nodeInfo"></param>
        /// <returns></returns>
        [SharePointCommand(ThemeGalleryCommandIds.GetThemeGalleryProperties)]
        private static Dictionary<string, string> GetThemeGalleryProperties(ISharePointCommandContext context,
            ThemeGalleryNodeInfo nodeInfo)
        {
            return SharePointCommandServices.GetProperties(context.Site.GetCatalog(SPListTemplateType.ThemeCatalog));
        }

        /// <summary>
        /// Get the default view url for the theme gallery
        /// </summary>
        /// <param name="context">The context</param>
        /// <returns>The default view of the theme gallery</returns>
        [SharePointCommand(ThemeGalleryCommandIds.GetThemeGalleryAllItemsUrl)]
        private static string GetThemeGalleryAllItemsUrl(ISharePointCommandContext context)
        {
            return context.Site.GetCatalog(SPListTemplateType.ThemeCatalog).DefaultViewUrl;
        }
    }
}
